home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


Now if the program executes the statement TotalItems(1, “cat”, 3), Visual Basic generates a very specific error message instead of a less meaningful generic type mismatch.

This subroutine examines all of its arguments before it begins any calculations. Because the checks are at the beginning of the subroutine, it is easy for you to later look at the code and verify that the checks are performed. It is also easy to change the parameter validations later if the argument list changes.

If the routine checked the argument types as it was adding them together, these tests would be buried deeper in the code. In this example, it would still be easy to find and modify the tests. In a more complex example, scattering the tests throughout the code would make the code harder to read. Putting all the tests at the beginning of the routine makes the code easier to understand.

Table 3.5 lists Visual Basic’s variant subtype functions that you can use to check variant data types.

Beware the One Form

When you define a form named MyForm, Visual Basic creates a template that you can use to make any number of forms of this type. For example, the following code creates and displays five instances of MyForm using the New statement.

Private Sub ShowFiveForms()
Dim i As Integer
Dim frm As MyForm

    For i = 1 To 5
        Set frm = New MyForm
        frm.Show
    Next i
End Sub

Visual Basic also creates a single special instance of the type MyForm that has the name MyForm. Many programmers confuse MyForm objects in general with this special form named MyForm. For example, the following code creates a new form. It then attempts to set the form’s caption and display the form. Unfortunately, it sets the caption for the one instance named MyForm instead.

Private Sub ShowOneForm()
Dim frm As MyForm
Table 3.5 Functions for Determining Variant Types
Function Purpose
IsArray True if an array.
IsDate True if a date or time.
IsEmpty True if uninitialized.
IsError True if an error value created by CVErr.
IsMissing True if an optional parameter that is missing.
IsNull True if null. This is not the same as uninitialized.
IsNumeric True if a numeric value.
IsObject True if an object.
VarType Returns a constant indicating the variant’s type.
    Set frm = New MyForm                   ‘ Create a new MyForm.
    MyForm.Caption = “My Form”             ‘ Set the caption of the one MyForm.
    frm.Show                               ‘ Display the new MyForm.
End Sub

As if this were not confusing enough, this code has also realized two copies of the form: one referenced by the frm variable and the original form named MyForm. If you close all of the visible forms after running this code, the special form named MyForm will still be running, even though it was never displayed. While no forms are visible, the program will not stop because the special instance of MyForm is still loaded. Be aware of this sort of confusion.

Another way the one form can cause problems is if a form’s code explicitly references its own type. For example, the following MyForm code attempts to set the form’s height and width when it is loaded. Unfortunately, it references the one form named MyForm rather than itself. Instead of resizing itself, the form resizes the one form. When the form appears, it will not have the correct size. This code has also referred to the one form so that form is loaded, even though it is probably not visible. If you close all visible windows, the one form will still be loaded so the program will not stop.

Private Sub Form_Load()
    MyForm.Width = 6000
    MyForm.Height = 2880
End Sub

To avoid this problem, never refer to a form’s type name within its own source code. Always refer to the current form implicitly as in this code:

Private Sub Form_Load()
    Width = 6000
    Height = 2880
End Sub

Better still, emphasize that the current form is the one you want to reference by using the Me keyword as in this code:

Private Sub Form_Load()
    Me.Width = 6000
    Me.Height = 2880
End Sub

Most programs either use the one form or they use only other forms created using New. If the program will only need one copy of the form, it can refer to the one form explicitly as in the following code:

UserNameForm.txtUserName = “”
UserNameForm.Show

If a program will need several copies of the form, it should not use the one form. It should create the new forms using New instead.

Dim frm As New MyForm

    frm.Show


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.